/**
 * 
 */
package com.calf.module.loan.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.calf.cn.controller.BaseController;
import com.calf.cn.entity.DataTables;
import com.calf.cn.utils.MyJedisPool;
import com.calf.cn.utils.SearchUtil;
import com.calf.module.loan.entity.AppInfo;

import net.sf.json.JSONObject;

/**
 * @ClassName AppInfoController
 * @Description Description
 * @author cx
 * @Date 2017年3月9日 下午8:36:24
 * @version 1.0.0
 */
@Controller
@RequestMapping("/appinfo")
public class AppInfoController implements BaseController<AppInfo> {

	/*
	 * (非 Javadoc) Description:
	 * 
	 * @see com.calf.cn.controller.BaseController#home()
	 */
	@Override
	public String home() {
		return "calfloan/app/info";
	}

	/*
	 * (非 Javadoc) Description:
	 * 
	 * @see com.calf.cn.controller.BaseController#initTable(javax.servlet.http.
	 * HttpServletRequest)
	 */
	@Override
	public DataTables<AppInfo> initTable(HttpServletRequest request) {
		Map<String, Object> parameters = SearchUtil
				.convertorEntitysToMap(request.getParameterMap());
		String sEcho = (String) parameters.get("sEcho");
		Object object = MyJedisPool.get("calfLoan_adUrl");
		JSONObject jsonObject = JSONObject.fromObject(object);
		AppInfo appInfo = new AppInfo();
		List<AppInfo> appInfos = new ArrayList<AppInfo>();
		if (jsonObject.containsKey("title")) {
			appInfo.setTitle(jsonObject.get("title").toString());
		}
		if (jsonObject.containsKey("image")) {
			appInfo.setImage(jsonObject.get("image").toString());
		}
		if (jsonObject.containsKey("url")) {
			appInfo.setUrl(jsonObject.get("url").toString());
		}
		if (jsonObject.containsKey("state")) {
			appInfo.setState(jsonObject.get("state").toString());
		}
		appInfos.add(appInfo);
		return new DataTables<AppInfo>(sEcho, appInfos, appInfos.size(),
				appInfos.size());
	}

	/*
	 * (非 Javadoc) Description:
	 * 
	 * @see
	 * com.calf.cn.controller.BaseController#addAndUpdateHome(org.springframework
	 * .ui.Model, java.lang.String)
	 */
	@Override
	public String addAndUpdateHome(Model model, String id) {
		Object object = MyJedisPool.get("calfLoan_adUrl");
		JSONObject jsonObject = JSONObject.fromObject(object);
		AppInfo appInfo = new AppInfo();
		appInfo.setImage(jsonObject.get("image").toString());
		appInfo.setTitle(jsonObject.get("title").toString());
		appInfo.setUrl(jsonObject.get("url").toString());
		appInfo.setState(jsonObject.get("state").toString());
		model.addAttribute("entity", appInfo);
		return "calfloan/app/updateApp";
	}

	/*
	 * (非 Javadoc) Description:
	 * 
	 * @see com.calf.cn.controller.BaseController#saveAdd(java.lang.Object)
	 */
	@Override
	public int saveAdd(AppInfo entity) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", entity.getTitle());
		map.put("url", entity.getUrl());
		map.put("image", entity.getImage());
		map.put("state", entity.getState());
		MyJedisPool
				.put("calfLoan_adUrl", JSONObject.fromObject(map).toString());
		return 1;
	}

	/*
	 * (非 Javadoc) Description:
	 * 
	 * @see com.calf.cn.controller.BaseController#delete(java.lang.Long)
	 */
	@Override
	public int delete(Long id) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (非 Javadoc) Description:
	 * 
	 * @see com.calf.cn.controller.BaseController#saveUpdate(java.lang.Object)
	 */
	@Override
	public int saveUpdate(AppInfo entity) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (非 Javadoc) Description:
	 * 
	 * @see com.calf.cn.controller.BaseController#delete(java.lang.String)
	 */
	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * @RequestMapping(value = "/home.htm") public String home(Model model) {
	 * Object object = MyJedisPool.get("calfLoan_adUrl"); JSONObject jsonObject
	 * = JSONObject.fromObject(object); AppInfo appInfo = new AppInfo();
	 * List<AppInfo> appInfos = new ArrayList<AppInfo>();
	 * appInfo.setImage(jsonObject.get("image").toString());
	 * appInfo.setTitle(jsonObject.get("title").toString());
	 * appInfo.setUrl(jsonObject.get("title").toString());
	 * appInfos.add(appInfo); return new DataTables<Banner>(sEcho, banners,
	 * banners.size(), total); // model.addAttribute("image",
	 * jsonObject.get("image").toString()); // model.addAttribute("title",
	 * jsonObject.get("title").toString()); // model.addAttribute("url",
	 * jsonObject.get("url").toString()); }
	 * 
	 * @MethodLog(operType = "修改")
	 * 
	 * @RequestMapping(value = "/update.htm") public void
	 * update(@RequestParam("appinfoFile") MultipartFile file, String url,
	 * String title, String image, HttpServletRequest request) { try {
	 * 
	 * Map<String, String> map = new HashMap<String, String>(); if
	 * (file.isEmpty()) { map.put("image", image); } if (!file.isEmpty()) {
	 * ServletContext context = request.getSession() .getServletContext();
	 * String webPath = context.getRealPath("/"); String imgurl = ""; String
	 * savepath = ""; int result = 0;
	 * 
	 * String uuid = UUIDUtils.getUUID().toLowerCase(); savepath = webPath +
	 * "image_space" + File.separator + uuid.substring(0, 3) + File.separator +
	 * uuid + ".jpg"; File dir = new File(savepath);
	 * 
	 * if (!dir.isDirectory()) { dir.mkdirs(); } try { file.transferTo(dir);
	 * 
	 * } catch (Exception e) { e.printStackTrace(); }
	 * 
	 * String dirs = uuid.substring(0, 3);
	 * 
	 * String path = ""; path = "/images/calfloan/loan/"; SFtpUtil sftp =
	 * SFtpUtil.getSFtpUtil(); sftp.createDir(path, dirs);
	 * 
	 * imgurl = "http://testloan.xnsudai.com/" + dirs + "/" + uuid + ".jpg";
	 * sftp.upload(path + dirs + "/", savepath, uuid + ".jpg"); map.put("image",
	 * imgurl); }
	 * 
	 * map.put("url", url); map.put("title", title); } catch (Exception e) {
	 * e.printStackTrace(); } }
	 */
}
